package com.it.od.od20221203;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * @author: liminghui
 * @date: 2022/12/3 14:32
 * @version: 1.0
 * @description: 字符串重新排序
 */
public class Main1 {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] arrStr = str.split(" ");
        // 1.单词内部调整
        List<String> strList = new ArrayList<>();
        for (String s : arrStr) {
            char[] chars = s.toCharArray();
            Arrays.sort(chars);
            strList.add(String.valueOf(chars));
        }
        // 2.单词间顺序调整
        Map<String, Integer> countMap = new HashMap<>();
        for (String s : strList) {
            if (countMap.containsKey(s)) {
                countMap.put(s, countMap.get(s) + 1);
            } else {
                countMap.put(s, 1);
            }
        }

        // 按次数排序
        List<Map.Entry<String, Integer>> entryList = new ArrayList<>(countMap.entrySet());
        // 比较器实现排序
        entryList.sort(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                // 先单词次数降序 -> 单词长度升序 ->字典序升序
                if (o1.getValue() > o2.getValue()) {
                    return -1;
                }
                if (o1.getValue().equals(o2.getValue())) {
                    if (o1.getKey().length() > o2.getKey().length()) {
                        return 1;
                    }
                    if (o1.getKey().length() == o2.getKey().length()) {
                        return o1.getKey().compareTo(o2.getKey());
                    }
                    return -1;
                }
                return 1;
            }
        });

        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry<String, Integer> entry : entryList) {
            Integer count = entry.getValue();
            for (int i = 0; i < count; i++) {
                stringBuilder.append(entry.getKey()).append(" ");
            }
        }

        System.out.println(stringBuilder.toString());

    }
}
